const MODEL_SCALE = 0.3084; //qmodel英尺转米
var _v1$2 = null;
var tmplen;


var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },
    // private method for UTF-8 encoding
    _utf8_encode: function (vk) {
        vk = vk.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < vk.length; n++) {

            var c = vk.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function (utftext) {
        var string = "";
        var i = 0;
        var c = 0;
        var c3 = 0;
        var c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}

var QmAnnotation = function (SVG, svgDemo, rectPosition) {
    this.isDrawing = false,
        this.draw = null,
        this.startX = null,
        this.startY = null,
        this.currentRect = null,
        this.rectangles = [],
        this.selectedRect = null,
        this.isMove = false;
    this.isStop = true;
    this.draw = svgDemo.appendChild(SVG()).size(rectPosition.width, rectPosition.height);
    this.draw.on('mousedown', this.onMouseDown.bind(this));
    this.draw.on('mousemove', this.onMouseMove.bind(this));
    this.draw.on('mouseup', this.onMouseUp.bind(this));

}
QmAnnotation.prototype = {

    startDraw() {
        this.isStop = false;
    },
    stopDraw() {
        this.isStop = true;
    },
    // 当鼠标按下时
    onMouseDown(e) {
        if (this.isStop) return;
        if (this.$el.style.cursor === 'move') {
            this.isMove = true;
        } else {
            if (e.button === 0) { // 鼠标左键
                this.isDrawing = true; // 开始绘制矩形
                this.startX = e.offsetX;
                this.startY = e.offsetY;
                this.currentRect = this.draw.rect().fill('none').stroke({ color: '#000', width: 2 }); // 创建SVG矩形元素
            }
        }

    },

    // 当鼠标移动时
    onMouseMove(e) {
        if (this.isStop) return;
        if (this.isDrawing) { // 如果正在绘制
            const currentX = e.offsetX;
            const currentY = e.offsetY;
            const width = Math.abs(currentX - this.startX);
            const height = Math.abs(currentY - this.startY);
            const x = Math.min(this.startX, currentX);
            const y = Math.min(this.startY, currentY);
            this.currentRect.attr({ x, y, width, height }); // 更新当前矩形的位置和大小
        } else { // 如果没有绘制
            // 检查鼠标是否在一个矩形上方
            let res = this.getRectFromMousePos(e.offsetX, e.offsetY) || '';
            if (res) {
                this.selectedRect = res.rect; // 选中该矩形
                this.$el.style.cursor = 'move'; // 更改光标样式
                this.updateRectColor(res.rect, 'red'); // 突出显示该矩形
                // 按下鼠标进行拖动
                if (this.isMove) {
                    this.rectangles[res.index].x = e.offsetX - this.selectedRect.width / 2;
                    this.rectangles[res.index].y = e.offsetY - this.selectedRect.height / 2;
                    this.rectangles[res.index].width = this.selectedRect.width;
                    this.rectangles[res.index].height = this.selectedRect.height;
                    this.redrawRectangles(); // 重新绘制所有矩形以反映对它们的更改
                }
            } else {
                if (this.selectedRect) {
                    this.$el.style.cursor = 'auto';
                    this.updateRectColor(this.selectedRect, '#000'); // 取消突出显示该false; // 标记为完成绘制
                    this.currentRect = null; // 重置当前矩形
                }
            }
        }
    },
    onMouseUp(e) {
        if (this.isStop) return;
        if (this.isMove && e.button === 0) { // 如果完成拖动
            this.$el.style.cursor = 'auto'; // 恢复光标样式
            this.isMove = false
            this.updateRectColor(this.selectedRect, '#000'); // 取消突出显示该矩形
            this.selectedRect = null; // 取消选择该矩形
        } else if (this.isDrawing) {
            this.finishRect(); // 完成绘制
        }
    },
    finishRect() {
        const rect = this.currentRect.bbox();
        this.rectangles.push({ x: rect.x, y: rect.y, width: rect.width, height: rect.height });
        this.isDrawing = false;
        this.currentRect = null;
    },
    // 清空画布
    clearCanvas() {
        this.rectangles = []; // 清空rectangles数组
        this.draw.clear(); // 清除SVG画布
    },

    // 保存所有矩形
    saveRectangles() {
        console.log(this.rectangles); // 打印到控制台
    },

    // 给定鼠标指针的位置，返回包含该点的第一个矩形
    // 增加鼠标移到矩形四个顶点附近，返回顶点坐标
    getRectFromMousePos(x, y) {
        for (let i = this.rectangles.length - 1; i >= 0; i--) { // 遍历已绘制的矩形，从后往前遍历以匹配z-index
            const range = 5;
            let res = {
                rect: {}, //选中的矩形
                index: 0,  //该矩形在rectangles的索引
                point: {}, // 矩形四个点
            }
            res.rect = this.rectangles[i];
            res.index = i;
            const { rect } = res;
            //鼠标在矩形内
            if (x >= rect.x - range && x <= rect.x + rect.width + range && y >= rect.y + range && y <= rect.y + rect.height - range) {
                //鼠标在四个顶点范围
                let rect0 = this.getRectRange(rect.x, rect.y, range);
                return res;
            }
        }
        return null;
    },
    // 获取四个顶点的范围
    getRectRange(x, y, range) {
        let rect = {};
        rect.x = x - range;
        rect.y = y + range;
        rect.width = range * 2;
        rect.height = range * 2;
        return rect
    },

    // 更改矩形的描边颜色
    updateRectColor(rect, color) {
        const svgRect = this.getSVGRectFromRect(rect);
        svgRect.stroke({ color });
    },

    // 给定一个矩形对象，返回与之匹配的SVG矩形元素
    getSVGRectFromRect(rect) {
        return this.draw.children()[this.rectangles.indexOf(rect)];
    },

    // 重新绘制所有矩形以反映对它们的更改
    redrawRectangles() {
        this.rectangles.forEach((rect) => {
            const svgRect = this.getSVGRectFromRect(rect);
            svgRect.attr(rect); // 更新SVG矩形元素的位置和大小
        });
    }

}
export { QmAnnotation }